package com.smedi.ismedi.authoritymanagement.core.domain.service.impl;

import com.smedi.ismedi.authoritymanagement.core.domain.aggregatemodel.TermRelationshipGroupType;
import com.smedi.ismedi.authoritymanagement.core.domain.repository.TermRelationshipGroupRepository;
import com.smedi.ismedi.authoritymanagement.core.domain.service.TermRelationshipGroupDomainService;
import com.smedi.ismedi.authoritymanagement.core.infrastructure.constant.CustomError;
import com.smedi.ismedi.web.starter.exception.LogicException;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: xulinjun
 * @Date: 2021-7-19 17:32
 */
@Slf4j
@Service
@AllArgsConstructor
public class TermRelationshipGroupDomainServiceImpl implements TermRelationshipGroupDomainService {
    private final TermRelationshipGroupRepository termRelationshipGroupRepository;

    @Override
    public TermRelationshipGroupType saveRolePermissionType(TermRelationshipGroupType data) {
        TermRelationshipGroupType basic;
        if (data.getTermRelationshipGroupTypeId() != null) {
            basic = termRelationshipGroupRepository.selectByTermRelationshipGroupTypeId(data.getTermRelationshipGroupTypeId());
            if (basic == null) {
                throw LogicException.raise(CustomError.TERM_RELATIONSHIP_TYPE_NOT_EXIST);
            }
        } else {
            basic = new TermRelationshipGroupType();
        }
        basic.setType(data.getType())
                .setMeaning(data.getMeaning())
                .setChildrenRules(data.getChildrenRules());

        // 保存数据
        TermRelationshipGroupType domain = (data.getTermRelationshipGroupTypeId() == null) ? termRelationshipGroupRepository.doInsert(basic) : termRelationshipGroupRepository.doUpdate(basic);
        return domain;
    }

    @Override
    public List<TermRelationshipGroupType> getAllRolePermissionTypes() {
        return termRelationshipGroupRepository.selectRolePermissionTypeList();
    }
}
